package com.atguigu.lease.admin.service.impl;

import com.atguigu.lease.admin.service.FileService;
import com.atguigu.lease.common.config.MinioConfig;
import com.atguigu.lease.common.util.FileType;
import com.atguigu.lease.common.util.FileUtil;
import io.minio.*;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;


import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.UUID;

@Service
public class FileServiceImpl implements FileService {

    @Autowired
    MinioClient minioClient;
    @Autowired
    MinioConfig minioConfig;
    @Override
    public String upload(MultipartFile file) {
        try {



            String filename = file.getOriginalFilename();
            String path = new SimpleDateFormat("yyyyMMdd").format(new Date())
                    +"/"+ System.currentTimeMillis() + "_" + UUID.randomUUID().toString().substring(0,6)
                    +filename.substring(filename.lastIndexOf("."));
            minioClient.putObject(PutObjectArgs.builder()
                            .bucket(minioConfig.getBucket())
                            .stream(file.getInputStream(), file.getInputStream().available(), -1)
                            .object(path)
                    .build());
            return minioConfig.getEndpoint()+"/"+minioConfig.getBucket()+"/"+path;

        } catch (Exception e) {
            throw new RuntimeException(e);
        }
    }
}
